﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Configuration;
using System.Net.Mail;
using System.Net.Sockets;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class EmailingWeeklyProgressReport : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
		if (!IsPostBack)
		{
			/*string message;
			DataSet ds = new DataLayer().GetEmailingWeeklyProgressReport(PAConfiguration.ConnectionString, out message);

			lblFullName.Text = string.Format("{0} {1}", ds.Tables[1].Rows[0]["FIRST_NAME"], ds.Tables[1].Rows[0]["LAST_NAME"]);
			lblGradeLevel.Text = ds.Tables[1].Rows[0]["GRADE_LEVEL"].ToString();
			lblCurrentDate.Text = DateTime.Today.ToShortDateString();*/
		}
    }

	/*protected override void Render(HtmlTextWriter writer)
	{
		StringBuilder sb = new StringBuilder();

		StringWriter sw = new StringWriter(sb);



		HtmlTextWriter hWriter = new HtmlTextWriter(sw);

		base.Render(hWriter);

		string pageResult = sb.ToString();

		

		//convert htmlPage to pdf

		ConvertToPdf(pageResult);

		//save pdf to server
		
		//sending page to email with pdf attachment

		string emailTo = "bykost@gmil.com";

		if (IsRealEmail(emailTo))
		{
			var msg = new MailMessage();

			msg.To.Add(new MailAddress(emailTo));
			msg.IsBodyHtml = true;
			msg.Body = pageResult;
			msg.Subject = "PA - Weekly Progress Report";

			if (SendEmail(msg))
			{
				//update delivered status in dp
			}
		}
		else
		{
			//do logic for bad email
		}

	}

	private bool SendEmail(MailMessage msg)
	{
		bool sendStatus = false;
		try
		{
			Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
			MailSettingsSectionGroup mailSettings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");

			msg.From = new MailAddress(mailSettings.Smtp.From);
			SmtpClient mailUserClient = new SmtpClient();
			mailUserClient.EnableSsl = Convert.ToBoolean(WebConfigurationManager.AppSettings["NotificationEmailEnableSSL"]);
			mailUserClient.Send(msg);

			sendStatus = true;
		}
		catch (Exception ex)
		{
			PALogManager.ExceptionLog(ex);
		}

		return sendStatus;

	}

	private bool IsRealEmail(string inputEmail)
	{
		bool isReal = false;
		try
		{
			string[] host = (inputEmail.Split('@'));
			string hostname = host[1];

			IPHostEntry IPhst = Dns.GetHostEntry(hostname);
			IPEndPoint endPt = new IPEndPoint(IPhst.AddressList[0], 25);
			Socket s = new Socket(endPt.AddressFamily,
			                      SocketType.Stream, ProtocolType.Tcp);
			s.Connect(endPt);
			s.Close();
			isReal = true;
		}
		catch (Exception ex)
		{
			PALogManager.ExceptionLog(ex);
		}

		return isReal;
	}

	private void ConvertToPdf(string html)
	{
		Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();

		var htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(html));

		pdf.BindHTML(htmlStream);

		pdf.Save("convertedHtml", Aspose.Pdf.SaveType.OpenInBrowser, Response);
	}*/


}